Routine, Sub-routine and Library routine
(Procedures & Function)



Learning Objectives : Student should be able to -

Procedures and Functions

Q1. a)  What is meant by  Routine  ?

⇒  A Routine is a general term used for the set of computer instructions which are executed to perform a task.

⇒  It is a section of a program, which could be either procedure, function, or subroutine.

b)  Describe  Sub-routine  and give reason why we use it.

✬  Subroutine is a named portion of large program that performs a specific task.

✬  Organising a big program into smaller sub-routines makes it easy to read and understand.

✬  Sub-routine could be reused at any stage of the main program.

✬  Sub-routine makes the program shorter and easier to write.

c)  Give  benefits  of using sub-routines.

  1. Easy to understand a big complex program by breaking it into small pieces.
  2. Reducing duplication of code by reusing the sub-routine.
  3. Reduce debugging time.
  4. Easier to modify the program.

Q2.  Procedures and Functions are sub-routines defined once, and can be called many times within a program. They make use of parameters.

Describe the following terms.

✬   Procedure : 

  1. Procedure is the set of instructions, grouped together under a single name, which can be called and executed to perform some task at any point in a program.
  2. Procudure do not returns any value.
  3. Procedure can call a function, but procedure can not be called by a function.

✬   Function : 

  1. Function is the set of instructions, grouped together under a single name, that process data with mathematical expression.
  2. It accepts the input value through parameters, calculates and returns back the result to the calling procedure or function.
  3. One function can call other function, but not a procedure.

✬   Parameter : 

  1. Parameter is a named variable declared within a procedure or function to provides data as input.
  2. They are the values of the arguments passed by the procedure or function while they are called or invoked.
  3. Some but not all procedures and functions will have parameters.

Q3. a)  Describe how procedures and functions are defined and used.

⇒  The first statement in the definition is a header, which contains :

⇒  Procedure and function call must match with the name and arguments of its definition.

⇒  Procedure calls are single standalone statements.

⇒  Function calls are made as part of an expression, on the right-hand side or could be used as part of an Output statement.

⇒  Procedure do not returns any value, but Function always returns back the result to the calling procedure or function.

b)  Define and use a Procedure using pseudocode to -

(i)  display three lines of stars without parameter.

PROCEDURE  Stars
FOR Count = 1 TO 3
OUTPUT  "✬ ✬ ✬ ✬ ✬ ✬ ✬ ✬"
NEXT Count
END PROCEDURE

The procedure can then be called in the main part of the program as many times as is required in the following way :

CALL  Stars

(ii)  accepts a parameter to display a given number of lines of stars.

PROCEDURE  Stars(Number : INTEGER)
FOR Count = 1 TO Number
OUTPUT  "✬ ✬ ✬ ✬ ✬ ✬ ✬ ✬"
NEXT Count
END PROCEDURE

The procedure call must match with the name and arguments of its definition in the following way :

MyNumber ‹— 7
CALL  Stars(MyNumber)

c)  Define and use a Function, using pseudocode, with a parameter, to convert a temperature from Celsius to Fahrenheit.

Temperature in fahrenheit, FTemp = ( CTemp * 9/5) + 32, where CTemp are temperature in celsius.

FUNCTION  ToFahrenheit(CTemp : REAL) RETURNS  REAL
TempF ‹— (CTemp * 1.8) + 32
RETURN  TempF
END FUNCTION

The keyword RETURN is used to specify the value to be returned. The function call are not standalone, they are used as part of an expression or programming statement on right-hand side in the following way :

ReceivedTemp ‹— ToFahrenheit(ConvertTemp)
OUTPUT  "Temperature in fahrenheit is ",  ReceivedTemp
/ OR /
OUTPUT  "Temperature in fahrenheit is ",  ToFahrenheit(ConvertTemp)

d)  State the difference between a Parameter and an Argument.

⇒  The variables that are defined within the procedure or function when it is declared is known as Parameter. It is the value that is sent from the main program to the subroutine (procedure or function).

⇒  The variables that are define within the statement that calls a procedure or function is known an Argument. It is the value that are passed while calling the subroutine (procedure or function).

Q4.  A function is declared and executed using pseudocode.

FUNCTION  ConvertToCm(Inches : REAL) RETURNS  REAL
RETURN  Inches * 2.4
END FUNCTION

INPUT  "Enter the length of object in centimeter is ",  Lenght
OUTPUT  "The length of object in inches is ",  ConvertToCm(Length)

a)  Describes the use of the variable Inches in the function declared above.

⇒  The variable Inches declared within the function when it is defined is called parameter.

⇒  It is used to receive data and provides it as input to the function for processing.

b)  Describes the use of the variable Length while calling and executing the function above.

⇒  The variable Length is called argument, which is used to pass the value to the parameter of the function, when the function is called or executed.

Q5.  The variable X, Y and Z are used in a program: X stores a whole number, Y stores a decimal number and Z stores a flag that can be set to TRUE or FALSE.

a)  Write pseudocode statement to declare the variables X, Y and Z.

DECLARE  X : INTEGER

DECLARE  Y : REAL

DECLARE  Z : BOOLEAN

b)  The function Same(A, B) returns TRUE if the value of A is the same as the value of B when B is rounded to the nearest whole number and FALSE otherwise.

Write pseudocode statement to:

Function definition : ...........................................................................

FUNCTION  Same(A : INTEGER, B : REAL) RETURNS  BOOLEAN
IF  A = ROUND(B)  THEN
RETURN  TRUE
ELSE
RETURN  FALSE
ENDIF
END FUNCTION

Function call : ........ Z = Same(X, Y) .................................................

c)  State the difference between defining and calling a function.

⇒  Function definition simply names the section of code with named parameters to pass as input data for processing.

⇒  Function call are made as part of an expression on the right-hand side, to execute and get the return value of the function.

⇒  Function call could be used as part of output statement to execute and display its return value.

Q6.  Write a procedure that takes two values as parameters and finds the remainder of value 1 divided by value 2.

You must also write the main program to allow input of the values and the line of code needed to call your procedure.

// Ask to input two values
OUTPUT  “Enter the value-1”
INPUT  Value1
OUTPUT  “Enter the value-2”
INPUT  Value2

// Call the procedure by passing input values as argument
CALL  FindRemainder(Value1, Value2)

// Defining the procedure to accept two parameter values and output result
PROCEDURE  FindRemainder(Num1 : INTEGER, Num2 : INTEGER)
// MOD is a library function to divide 2 values and return its remainder.
Result = Num1 MOD Num2
OUTPUT  “The remainder of value-1 divided by value-2 is ”, Result
END PROCEDURE

Q7.  Write a function that :

You must also write the main program to allow input of the values and the line of code needed to call and output the return value of your function.

// Ask to input first name and last name
OUTPUT  “Enter the first name : ”
INPUT  FName
OUTPUT  “Enter the last name : ”
INPUT  LName

// Call the function by passing input values as argument and output its return value
UserName ‹— ExtractUserName(FName, LName)
OUTPUT  “Your user name is ”, UserName

// Defining the function to accept two parameter values and return the result
FUNCTION  ExtractUserName(N1 : STRING, N2 : STRING) RETURNS STRING
Extract1 ‹— SUBSTRING(N1, 1, 2)
Extract2 ‹— SUBSTRING(N2, 1, 3)
Result ‹— Extract1 + Extract2 + "01"
RETURN  Result
END FUNCTION

Q8.  Write an algorithm that meets the following requirements :

Perform the task of the options by calling an appropriately named procedure or function.

REPEAT
CALL  DashBoard
OUTPUT  "Input your choice (between 1 and 4) : "
INPUT  Option

CASE  Option OF
1 :
INPUT  "Enter the radius of circle : ",  Radius
OUTPUT  "The area of circle is ",  AreaCircle(Radius)
2 :
INPUT  "Enter the length of rectangle : ",  Length
INPUT  "Enter the width of rectangle : ",  Width
OUTPUT  "The area of rectangle is ",  AreaRectangle(Length, Width)
3 :
INPUT  "Enter the base of the triangle : ",  Base
INPUT  "Enter the height of the triangle : ",  Height
OUTPUT  "The area of triangle is ",  AreaTriangle(Base, Height)
4 :
OUTPUT  "Thanks of using the program."
OTHERWISE
OUTPUT  "Invalid, re-enter the option."
END CASE
UNTIL  Option = 4

// Procedure to display the available options to choose
PROCEDURE  DashBoard
OUTPUT  "The list of available options."
OUTPUT  "1 :  Find the area of the circle."
OUTPUT  "2 :  Find the area of the rectangle."
OUTPUT  "3 :  Find the area of the triangle."
OUTPUT  "4 :  Quit."
END PROCEDURE

// Function to calculate and return the area of circle.
FUNCTION  AreaCircle(R : REAL) RETURNS REAL
ACircle ‹— 3.142 * R * R
RETURN  ACircle
END FUNCTION

// Function to calculate and return the area of rectangle.
FUNCTION  AreaRectangle(L : REAL, W : REAL) RETURNS REAL
ARect ‹— L * W
RETURN  ARect
END FUNCTION

// Function to calculate and return the area of triangle.
FUNCTION  AreaTriangle(B : REAL, H : REAL) RETURNS REAL
ATr ‹— 0.5 * B * H
RETURN  ATr
END FUNCTION

►   Local and Global Variables

Q1.  Programs can use both local and global variables.

Describe, using examples, the difference between  Local  and  Global variables .

⇒  Global variables are defined outside the function or procedure which can be accessed globally from anywhere in the entire program.

⇒  Whereas, Local variables can be accessed only within the function or procedure in which they are defined.

⇒  It is possible to have local variables with the same name in different functions.

For example, in this algorithm the variable GrandTotal is declared globally outside any procedures or functions, whereas variable Count is declared locally within the procedure.

DECLARE GrandTotal : INTEGER

PROCEDURE MarkEntry
DECLARE Count : INTEGER
FOR Count = 1 to 30
- - - - - - - - - - - - -
END PROCEDURE

Q2.  Write an algorithm that meets the following requirements :

Perform the task for each option by calling an appropriately named procedure or function.

DECLARE Password : STRING

REPEAT
CALL  DashBoard
OUTPUT  "Input your choice (between 1 and 4) : "
INPUT  Option

CASE  Option OF
1 :
CALL  NewPwd
2 :
INPUT  "Enter the password to check : ",  CheckPass
OUTPUT  "Your password is ",  PassChecker(CheckPass)
3 :
INPUT  "Enter your old password : ",  OldPass
IF  PassChecker(CheckPass) = "Correct" THEN
CALL  NewPwd
OUTPUT  "Your password is changed successfully"
ELSE
OUTPUT  "Your old password is wrong"
ENDIF
4 :
OUTPUT  "Thanks of using the program."
OTHERWISE
OUTPUT  "Invalid, re-enter the option."
END CASE
UNTIL  Option = 4

// Procedure to display the available options to choose
PROCEDURE  DashBoard
OUTPUT  "Choose the option from the list below -"
OUTPUT  "1 :  Enter a new password."
OUTPUT  "2 :  Check the password."
OUTPUT  "3 :  Change the password."
OUTPUT  "4 :  Quit."
END PROCEDURE

// Procedure to enter and store new password.
PROCEDURE  NewPwd
INPUT  "Enter your new password : ",  NewPass
Password ‹— NewPass
OUTPUT  "New password is added successfully"
END PROCEDURE

// Function to check the password and return the result as string.
FUNCTION  PassChecker(PassToCheck : STRING) RETURNS STRING
IF  Password = PassToCheck THEN
RETURN  "Correct"
ELSE
RETURN  "Wrong"
ENDIF
IF  Password = "" THEN  RETURN  "not defined."
END FUNCTION

►   Library routines

Q1. a)  Describe what is meant by  Library Routine  with an example.

✬  A library routine is a ready-made debugged block of code, designed to handle commonly occurring problems or tasks.

✬  Library routines are stored in a program library and given names.

✬  This allows them to be called into immediate use when needed by any programs.

✬  They are designed to be used frequently.

Example :   MOD’, is a library routine, already written and tested mathematical function, which returns the remainder when a number is divided by another, like; R = 5 MOD 2, which will return the value 1.

b)  Explain why programmers find library routines useful when writing programs.

✬  Using library routines make writing programs faster and easier as part of the work has already been done.

✬  They don't need any testing or debugging.

c)  Give two  difference  between  Sub-routine  and  Library-routine. 

  1. Library routine is a pre-written and debugged codes, whereas Sub-routine is the part of program being developed.
  2. Library routine can be used by any number of programs, whereas Sub-routine can be used only by the program to which it belongs to.

Q2.  Describe the following library routines that programmers frequently use.

 MOD   : It returns the remainder of a division.

Example :  MOD(9, 2) returns 1, the remainder of 9 divided by 2.

 DIV   : It returns the quotient (i.e. the whole number part) of a division.

Example :  DIV(9, 2) returns 4, the quotient of 9 divided by 2.

 ROUND   : It returns a value rounded to a given number of decimal places.

Example :  ROUND(6.97354, 2) returns 6.97, the rounded value to 2 decimal places.

 RANDOM   : It returns a random number between 0 and 1 inclusive.

Example :  RANDOM() returns a value in the range 0, 0.001, 0.002, ... 0.9, 1.

So, to get a random real number (with decimal values) between 1 and 5, we have to use the library function - RANDOM()*(5-1)+1

and to get a random whole number between 1 and 5, we have to include the library function ROUND, like - ROUND(RANDOM()*4+1,  0).

Q3.  Write an algorithm using pseudocode to input a number, find and output whether it is an even or an odd number.

OUTPUT  "Enter a number :"
INPUT  Num
IF  MOD(Num, 2) = 0 THEN
OUTPUT  "It is an Even number."
ELSE
OUTPUT  "It is an Odd number."
ENDIF

Q4.  Write an algorithm using pseudocode to input the length of steel pipe in inch, find and output how many pieces of 6 inch pipes we could get out of it.

OUTPUT  "Enter the length of pipe in inches :"
INPUT  Length
NoPipes ‹— DIV(Length, 6)
OUTPUT  NoPipes, " number of pieces can be taken out of it."

Q5.  Write an algorithm using pseudocode to input the radius of circle, calculate and output its area rounded to 2 decimal places.

OUTPUT  "Enter the radius of the circle :"
INPUT  Radius
Area ‹— 3.142 * Radius * Radius
RoundArea ‹— ROUND(Area,  2)
OUTPUT  "Area of circle is ", RoundArea

Q6.  Write an algorithm using pseudocode to input the starting number and ending number of lottery ticket, and output the ticket number of winner selected in random as result.

INPUT  "Enter starting lottery number :",  StartNo
INPUT  "Enter ending lottery number :",  EndNo
Result ‹— ROUND(RANDOM() * (EndNo - StartNo) + StartNo,   0)
OUTPUT  "Winner's Lottery number is ", Result



* * * * * * * * *
* * * * * *
* * *
*